using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls;

using CMS.ExtendedControls;
using CMS.FormControls;
using CMS.GlobalHelper;

/// <summary>
/// Text area form control with syntax highlighting suppor.
/// </summary>
public partial class CMSFormControls_Inputs_LargeTextArea : FormEngineUserControl
{
    #region "Variables"

    private bool mAllowMacros = true;

    #endregion


    #region "Properties"

    /// <summary>
    /// Gets or sets whether macros are allowed.
    /// </summary>
    /// <value>True, if macros can be inserted, otherwise false</value>
    [Browsable(true)]
    [Description("Determines whether macros are allowed")]
    [Category("Form Control")]
    [DefaultValue(true)]
    public bool AllowMacros
    {
        get
        {
            return (Form != null ? Form.AllowMacroEditing : mAllowMacros);
        }
        set
        {
            mAllowMacros = value;
        }
    }


    /// <summary>
    /// Gets the ExtendedTextArea object used in this form control.
    /// </summary>
    [Browsable(false)]
    public ExtendedTextArea TextArea
    {
        get
        {
            return txtArea;
        }
    }


    /// <summary>
    /// Gets or sets whether this form control is enabled.
    /// </summary>
    /// <value>True, if form control is enabled, otherwise false</value>
    [Browsable(true)]
    [Description("Determines whether this form control is enabled")]
    [Category("Form Control")]
    [DefaultValue(true)]
    public override bool Enabled
    {
        get
        {
            return base.Enabled;
        }
        set
        {
            base.Enabled = txtArea.Enabled = btnMore.Enabled = value;
        }
    }


    /// <summary>
    /// Gets the server control identifier generated by ASP.NET.
    /// </summary>
    [Browsable(false)]
    public override string InputClientID
    {
        get
        {
            return txtArea.ClientID;
        }
    }


    /// <summary>
    /// Gets or sets the value of this form control.
    /// </summary>
    /// <value>Text content of this editor</value>
    [Browsable(false)]
    public override object Value
    {
        get
        {
            return txtArea.Text;
        }
        set
        {
            txtArea.Text = (string)value;
        }
    }


    /// <summary>
    /// Gets the client ID of the embedded element that provides the value for this control.
    /// </summary>
    [Browsable(false)]
    public override string ValueElementID
    {
        get
        {
            return txtArea.ClientID;
        }
    }


    /// <summary>
    /// Gets or sets the width of this control.
    /// </summary>
    /// <value>The width in pixels or percentage</value>
    [Browsable(true)]
    [Description("Determines the width of this control")]
    [Category("Appearance")]
    [DefaultValue("250px")]
    public Unit Width
    {
        get
        {
            return txtArea.Width;
        }
        set
        {
            txtArea.Width = value;
        }
    }


    /// <summary>
    /// Gets or sets the height of this control.
    /// </summary>
    /// <value>The height in pixels or percentage</value>
    [Browsable(true)]
    [Description("Determines the width of this control")]
    [Category("Appearance")]
    [DefaultValue("50px")]
    public Unit Height
    {
        get
        {
            return txtArea.Height;
        }
        set
        {
            txtArea.Height = value;
        }
    }


    /// <summary>
    /// Gets the button that opens the full-featured editor.
    /// </summary>
    public Button MoreButton
    {
        get
        {
            return btnMore;
        }
    }

    #endregion


    #region "Methods"

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event</param>
    /// <param name="e">The System.EventArgs instance containing the event data</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckMinMaxLength = true;
        CheckRegularExpression = true;

        // Register general dialog scripts
        ScriptHelper.RegisterDialogScript(Page);

        // Assign handler that opens modal dialog
        btnMore.OnClientClick += string.Format("ShowLargeTextAreaDesigner('{0}', {1}); return false;", ValueElementID, AllowMacros.ToString().ToLower());

        // Register modal dialog script
        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "ShowLargeTextAreaDesigner", GetDesignerScript());

        // Register script that gets/sets values between control and modal dialog
        ScriptHelper.RegisterClientScriptBlock(this, typeof(string), "AreaGlobal", GetValueTransferScript());
    }


    /// <summary>
    /// Returns the collection of the field IDs (Client IDs of the inner controls) that should be spell checked.
    /// </summary>
    /// <returns>Arraylist of elements to check with a spellchecker.</returns>
    public override ArrayList GetSpellCheckFields()
    {
        // There is nothing to check - this editor is not used for live site text content
        return new ArrayList();
    }


    /// <summary>
    /// Builds and returns a client script that is used to open modal dialog with an editor in advanced mode.
    /// </summary>
    /// <returns>String that represents a modal dialog JS script.</returns>
    private string GetDesignerScript()
    {
        string script = string.Format(
            @"function ShowLargeTextAreaDesigner(editorID, allowMacros) 
              {{
                modalDialog('{0}?editorid=' + editorID + (allowMacros ? '' : '&allowMacros=false'), 'ShowLargeTextAreaDesigner', 1034, {1}); return false;
              }}",
            ResolveUrl("~/CMSFormControls/Selectors/LargeTextAreaDesigner.aspx"),
            (680 - (AllowMacros ? 0 : 60))
            );

        return ScriptHelper.GetScript(script);
    }


    /// <summary>
    /// Builds and returns a client script that is used to transfer the text between this editor and dialog window.
    /// </summary>
    /// <returns>String that represents a value transfer script.</returns>
    private string GetValueTransferScript()
    {
        // Derelict, needs cleanup
        const string script = @"function SetAreaValue(areaId, areaText){ 
                              document.getElementById(areaId).value = areaText; 
                              return false; 
                          }
                          function GetAreaValue(areaId){ 
                              return document.getElementById(areaId).value; 
                              return false;  
                          }";

        return ScriptHelper.GetScript(script);
    }

    #endregion
}